package com.hxkj.config;

import com.hxkj.interceptor.LoginAuthorArgumentResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.*;

import java.util.List;

/**
 * 拦截器注册
 * @author chenwei
 * @version 1.0
 * @datetime 2017/2/28 17:55
 */
@Service("webMvcConfiguration")
public class WebMvcConfiguration extends WebMvcConfigurerAdapter {

    @Autowired
    private LoginAuthorArgumentResolver loginAuthorArgumentResolver;


    @Override
    public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
        configurer.setTaskExecutor(mvcTaskExecutor());
        configurer.setDefaultTimeout(200 * 1000L);
        super.configureAsyncSupport(configurer);
    }

    @Bean
    public ThreadPoolTaskExecutor mvcTaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(10);
        executor.setMaxPoolSize(200);
        executor.setQueueCapacity(40000);
        executor.initialize();
        return executor;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 认证拦截器
//        InterceptorRegistration authInterceptor = registry.addInterceptor(new AuthHandlerInterceptor());
        // 配置拦截的路径
//        authInterceptor.addPathPatterns("/**");
        // 配置不拦截的路径
//        authInterceptor.excludePathPatterns("/user/**");

        // 权限拦截器
//        InterceptorRegistration functionInterceptor = registry.addInterceptor(new FunctionHandlerInterceptor());
        // 配置拦截的路径
//        functionInterceptor.addPathPatterns("/**");

        super.addInterceptors(registry);
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                //.allowedHeaders("Content-Type", "accessToken")
                //.exposedHeaders("Content-Type", "accessToken")
                .allowCredentials(true);
        //super.addCorsMappings(registry);
    }


    private static final String[] CLASSPATH_RESOURCE_LOCATIONS =
            {
                    "classpath:/templates/",
                    "classpath:/static/"
            };

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        if (!registry.hasMappingForPattern("/webjars/**")) {
            registry.addResourceHandler("/webjars/**").addResourceLocations(
                    "classpath:/META-INF/resources/webjars/");
        }
        if (!registry.hasMappingForPattern("/**")) {
            registry.addResourceHandler("/**").addResourceLocations(
                    CLASSPATH_RESOURCE_LOCATIONS);
        }
        super.addResourceHandlers(registry);
    }


    /**
     * 自定义参数拦截器
     *
     * @param argumentResolvers
     */
    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        super.addArgumentResolvers(argumentResolvers);
        argumentResolvers.add(loginAuthorArgumentResolver);
    }
}
